//
//  NearIndustryViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/17.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "NearIndustryViewController.h"
//#import "TestViewController.h"
#import "XLSlideSwitch.h"
#import "AllViewController.h"
#import "IndustryTypeModel.h"

@interface NearIndustryViewController ()<XLSlideSwitchDelegate>
@property(nonatomic,strong)XLSlideSwitch *slideSwitch;
@property(nonatomic,strong)NSMutableArray *titlesArr;
@end

@implementation NearIndustryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self buildUI];
    [self backToTop];
    //行业列表
    [self loadData];
}
-(void)backToTop{
    UIButton *toTopButton = [UIButton buttonWithType:UIButtonTypeCustom];
    toTopButton.frame = CGRectMake(Main_Screen_Width-52, Main_Screen_Height-91-110, 42, 42);
    [toTopButton setImage:[UIImage imageNamed:@"button_zhiding_nor"] forState:UIControlStateNormal];
    [toTopButton addTarget:self action:@selector(BackAction) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:toTopButton];
    [self.view bringSubviewToFront:toTopButton];
}

-(void)BackAction{
//    NSIndexPath *lastIndexPath=[NSIndexPath indexPathForRow:0 inSection:0];
//    [_tableV scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

}

-(void)loadData{
    //2.64附近获取行业列表
    self.titlesArr = [NSMutableArray array];
    __weak typeof(self) weakSelf = self;
    [Tools AFNetworkingRequestTypePostWithUrl:GETINDUSTRY paramters:nil afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"附近获取行业列表----%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *data = resultDic[@"data"];
            if([data isKindOfClass:[NSNull class]]) return ;
            IndustryTypeModel *model = [[IndustryTypeModel alloc]init];
            model.name = @"全部";
            model.id = @"";
            [weakSelf.titlesArr addObject:model];
            for(NSDictionary *dic in data){
                IndustryTypeModel *model = [[IndustryTypeModel alloc]init];
                model.name = dic[@"name"];
                model.id = dic[@"id"];
                [weakSelf.titlesArr addObject:model];
            }
            [weakSelf buildUI];
        }
    }];
}

-(void)buildUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    NSMutableArray *viewControllers = [NSMutableArray new];
//    NSArray *titles = @[@"全部",@"家具",@"服装",@"汽车",@"丽人",@"箱包",@"饰品",@"母婴",@"家电",@"丽人",@"箱包",@"更多"];
        for (int i = 0 ; i<self.titlesArr.count; i++) {
            AllViewController *vc = [AllViewController new];
            IndustryTypeModel *model = self.titlesArr[i];
            vc.title = model.name;
            vc.industryId = model.id;
            vc.vcIndex = i;
        [viewControllers addObject:vc];
    }
    _slideSwitch = [[XLSlideSwitch alloc] initWithFrame:CGRectMake(0, 2, Main_Screen_Width, Main_Screen_Height - kNaviBarHeight-49-2)];
    _slideSwitch.delegate = self;
    _slideSwitch.btnSelectedColor = DEF_COLOR;
    _slideSwitch.btnNormalColor = [UIColor colorWithRed:136./255 green:136./255 blue:136./255 alpha:1.];
    //    _slideSwitch.adjustBtnSize2Screen = true;
    _slideSwitch.viewControllers = viewControllers;
    [self.view addSubview:_slideSwitch];
    _slideSwitch.selectedIndex = 0;
}

#pragma mark -
#pragma mark SlideSwitchDelegate

-(void)slideSwitchDidselectTab:(NSUInteger)index
{
    //可以通过viewWillAppear方法加载数据
    AllViewController * vc = _slideSwitch.viewControllers[index];
    IndustryTypeModel *model = self.titlesArr[index];
    vc.industryId = model.id;
    [vc loadData];
}

-(void)slideSwitchPanLeftEdge:(UIPanGestureRecognizer *)panParam
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
}


@end
